<template>
  <div>
    <el-dialog
      :title="info.isAdd ? '添加管理员' : '修改管理员'"
      :visible.sync="info.isShow"
      width="60%"
      @close="cancel"
    >
      <el-form
        :model="ruleForm"
        :rules="rules"
        ref="ruleForm"
        label-width="100px"
        class="demo-ruleForm"
      >
        <el-form-item prop="specsname" label="属性名">
          <el-input v-model="ruleForm.specsname"></el-input>
        </el-form-item>
        <el-form-item label="属性值" prop="attrs">
          <div v-for="(item, index) in attrsList" :key="index">
            <el-input v-model="attrsList[index]"></el-input>
            <el-button v-if="index === 0" @click="addAttrs">添加属性</el-button>
            <el-button v-else @click.prevent="removeAttrs(index)"
              >删除</el-button
            >
          </div>
        </el-form-item>
        <el-form-item label="状态" prop="status">
          <!-- label的值需要是数字，因此加上冒号，保证是变量 -->
          <el-radio v-model="ruleForm.status" :label="1">正常</el-radio>
          <el-radio v-model="ruleForm.status" :label="2">禁用</el-radio>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="cancel()">取 消</el-button>
        <el-button type="primary" @click="submitForm('ruleForm')"
          >确 定</el-button
        >
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { specsedit, specsAdd } from "../../request";
let defaultData = {
  status: 1,
  attrs: "",
  specsname: "",
};
export default {
  props: {
    info: {
      type: Object,
      default: {
        isAdd: false,
        isShow: false,
      },
    },
  },
  data() {
    return {
      //属性值的数组
      attrsList: [""],
      //表单数据
      ruleForm: {
        status: 1,
        attrs: "",
        specsname: "",
      },

      //表单规则
      rules: {
        username: [
          { required: true, message: "管理员名称必填", trigger: "blur" },
        ],
        roleid: [{ required: true, message: "角色名称必填", trigger: "blur" }],
      },
    };
  },
  methods: {
    //增加一项
    addAttrs() {
      this.attrsList.push("");
    },
    removeAttrs(index) {
      this.attrsList.splice(index, 1);
    },
    submitForm() {
      this.$refs.ruleForm.validate((valid) => {
        if (valid) {
          let arr = [];
          this.attrsList.forEach((item) => {
            arr.push(item);
          });
          this.ruleForm.attrs = arr.join();
          // console.log(this.ruleForm);
          // return
          let res;
          if (this.info.isAdd) {
            res = specsAdd(this.ruleForm);
          } else {
            res = specsedit(this.ruleForm);
          }
          if (res) {
            this.cancel();
            this.$bus.$emit("update");
          }
        } else {
          return false;
        }
      });
    },
    resetForm(formName) {
      this.$refs[formName].resetFields();
    },
    //设置组件状态
    setStatus(val) {
      this.ruleForm = { ...val };
      //给属性值的数组赋值
      this.attrsList = this.ruleForm.attrs;
      //attrs后台需要字符串格式
      this.ruleForm.attrs = this.attrsList.join();
    },
    //取消按钮
    cancel() {
      this.ruleForm = { ...defaultData };
      //取消后清理值，重置attrsList 方便重新输入
      this.attrsList = [""];
      // 取消按钮后弹窗关闭
      this.info.isShow = false;
    },
  },
};
</script>

<style>
</style>